//
//  STOAuthController.m
//  OCVVeibo
//
//  Created by leergou on 16/7/21.
//  Copyright © 2016年 WhiteHouse. All rights reserved.
//

#import "STOAuthController.h"
#import "STProgressView.h"
#import "STUserAccountDTO.h"
#import <YYModel/YYModel.h>
#import "STUserAccountVM.h"

@interface STOAuthController ()<UIWebViewDelegate>

@property (nonatomic,strong) UIWebView *webView;

@property (nonatomic,strong) STProgressView *progressView;

@property (nonatomic,strong) STUserAccountVM *userAccountVm;

@property (nonatomic,strong) STUserAccountVM *userAccountVM;

@end

@implementation STOAuthController

#pragma mark - 入口
- (void)loadView{
    [super loadView];
    self.view = self.webView;
    self.view.backgroundColor = [UIColor whiteColor];
    // 加载新浪微博授权页
    NSString *urlString = [NSString stringWithFormat:@"https://api.weibo.com/oauth2/authorize?client_id=%@&redirect_uri=%@",APPKEY,APPREDIRECT_URI];
    
    NSURLRequest *requst = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:urlString]];
    
    if (requst) {
        [self.webView loadRequest:requst];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.progressView removeProgressView];
}

#pragma mark - 按钮点击事件
- (void)cancelClick:(id)sender{
    
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
}

- (void)autoFillClick:(id)sender{
    
    NSString *jsString = [NSString stringWithFormat:@"document.getElementById('userId').value='%@',document.getElementById('passwd').value='%@'",SinaAccount,SinaPassword];
    // js 注入
    [self.webView stringByEvaluatingJavaScriptFromString:jsString];
}

#pragma mark - webViewDelegate
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    
    NSString *urlString = request.URL.absoluteString; // 即开始拼接的 url字符串
    
    if ([urlString hasPrefix:APPREDIRECT_URI]) {
        
        NSString *queryString = request.URL.query;
        
        NSString *code = [queryString substringFromIndex:5];
        
        [self.userAccountVM loadUserAccountDataWithCode:code isSuccess:^(BOOL isSuccess) {
            
            if (isSuccess) {
                
                // 取消回调页
                [self.navigationController dismissViewControllerAnimated:NO completion:^{
                    // 发送通知,app delegate 切换根控制器
                    [[NSNotificationCenter defaultCenter] postNotificationName:STSwitchRootVCNotification object:nil];
                }];
            }
        }];
        return NO;
    }
    return YES;
}
- (void)webViewDidStartLoad:(UIWebView *)webView{
    
    [self.progressView startAnimation];
//    [SVProgressHUD show];
//    [SVProgressHUD setDefaultMaskType:(SVProgressHUDMaskTypeBlack)];
//    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleCustom];
//    [SVProgressHUD setBackgroundColor:ThemeColor];
//    [SVProgressHUD setForegroundColor:[UIColor whiteColor]];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView{
//    [SVProgressHUD dismiss];
    [self.progressView endAnimation];
    
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(nullable NSError *)error{}

#pragma mark - setupUI
- (void)setupUI{
    
    [self.navigationController.view insertSubview:self.progressView belowSubview:self.navigationController.navigationBar];
    
    [self setupNav];
}

#pragma mark - setupNav
- (void)setupNav{
    
    self.navigationItem.title = @"微博登录";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:(UIBarButtonItemStylePlain) target:self action:@selector(cancelClick:)];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"自动填充" style:(UIBarButtonItemStylePlain) target:self action:@selector(autoFillClick:)];
}

#pragma mark - lazy
- (UIWebView *)webView{
    if (!_webView) {
        _webView = [[UIWebView alloc] init];
        _webView.delegate = self;
    }
    return _webView;
}

- (STProgressView *)progressView{
    
    if (!_progressView) {
        _progressView = [[STProgressView alloc] init];
    }
    return _progressView;
}

- (STUserAccountVM *)userAccountVM{
    
    if (!_userAccountVM) {
        
#warning - 第一次这里实例化用的是  [[STUserAccountVM alloc] init]导致单例失效,最终在baseController 中引用 STUserAccountVM 的 isLogin 属性时,出现了一个新的对象,而新对象内部数据都为空,导致了第一次启动,授权完成后,控制器跳转不到登录页面
        _userAccountVM = [STUserAccountVM sharedUserAccountTools];

        
    }
    return _userAccountVM;
}


@end











